package com.lgy.model;

import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;
@Entity
@Table(name = "teacher")
public class Teacher implements Serializable {
	private static final long serialVersionUID = 6710350900667031491L;

	@Id
	@NotNull
	@GeneratedValue(strategy = GenerationType.AUTO)
	private int id;

	private String name;

	private String gender;

	// ----------------------------与学生之间多对多关系------------------------
	@ManyToMany(fetch = FetchType.LAZY)
	@JoinTable(name = "teacher_stu", joinColumns = {
					@JoinColumn(name = "teachid", referencedColumnName = "id") }, inverseJoinColumns = {
					@JoinColumn(name = "stuid", referencedColumnName = "id") })
	private Set<Student> students = new HashSet<Student>();;

	/**
	 * @return the id
	 */
	public int getId() {
		return id;
	}

	/**
	 * @param id
	 *            the id to set
	 */
	public void setId(int id) {
		this.id = id;
	}

	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}

	/**
	 * @param name
	 *            the name to set
	 */
	public void setName(String name) {
		this.name = name;
	}

	/**
	 * @return the gender
	 */
	public String getGender() {
		return gender;
	}

	/**
	 * @param gender
	 *            the gender to set
	 */
	public void setGender(String gender) {
		this.gender = gender;
	}
	
	/**
	 * @return the students
	 */
	public Set<Student> getStudents() {
		return students;
	}

	/**
	 * @param students the students to set
	 */
	public void setStudents(Set<Student> students) {
		this.students = students;
	}
	
	
	 /**
     * 添加学生
     * 
     * @param student
     */
    public void addStudent(Student student) {
        if (!this.students.contains(student)) {
            this.students.add(student);
            //student.setTeacher(this);
        }
    }

    /**
     * 删除学生
     * 
     * @param student
     */
    public void removeStudent(Student student) {
        if(this.students.contains(student)){
            //student.setTeacher(null);
            this.students.remove(student);
        }
    }
    
	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		return "Teacher [id=" + id + ", name=" + name + ", gender=" + gender + "]";
	}
}
